package jsal.qplayer.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import at.markushi.ui.ActionView;
import jsal.qplayer.R;

/*
* MainActivity:主界面
* 描    述：app的第一界面，仿QQ音乐
* 作    者：Jsal
* 时    间：2016-03-22
* 版    权：Jsal（不包括界面设计）
*/
public class MainActivity extends Activity implements View.OnClickListener {
    //以下是界面组件引用;
    private ImageButton imgb_menu_more;
    private TextView txt_wo_de;
    private TextView txt_yin_yue_guan;
    private TextView txt_fa_xian;
    private ImageButton imgb_nav_search;
    private RelativeLayout reltv_search_bar;
    private ImageButton imgb_search_speech;
    private RelativeLayout reltv_login_bar;
    private Button btn_main_login;
    private ActionView actionView;
    private ViewPager viewPager;
    //以下是容器引用
    private ArrayList<View> pages;//页面滑动容器
    private View page1;//页面内容容器
    private View page2;
    private View page3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actv_main);
        initWidgets();//获得所有组件的引用
        initViewPager();
        bindListeners();
    }


    //所有点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            /*viewpager滑动控制:点击文字滑向相应的页面*/
            case R.id.txt_wo_de:
                if (viewPager.getChildAt(0) != null) {
                    viewPager.setCurrentItem(0, true);
                } else Log.e("UI_ERR", "page 1 not found.");
                break;
            case R.id.txt_yin_yue_guan:
                if (viewPager.getChildAt(1) != null) {
                    viewPager.setCurrentItem(1, true);
                } else Log.e("UI_ERR", "page 2 not found.");
                break;
            case R.id.txt_fa_xian:
                if (viewPager.getChildAt(2) != null) {
                    viewPager.setCurrentItem(2, true);
                } else Log.e("UI_ERR", "page 3 not found.");
                break;
        }
    }

    //页面组件获取
    private void initWidgets() {
        imgb_menu_more = (ImageButton) findViewById(R.id.imgb_menu_more);
        txt_wo_de = (TextView) findViewById(R.id.txt_wo_de);
        txt_yin_yue_guan = (TextView) findViewById(R.id.txt_yin_yue_guan);
        txt_fa_xian = (TextView) findViewById(R.id.txt_fa_xian);
        imgb_nav_search = (ImageButton) findViewById(R.id.imgb_nav_search);
        reltv_search_bar = (RelativeLayout) findViewById(R.id.reltv_search_bar);
        imgb_search_speech = (ImageButton) findViewById(R.id
                .imgb_search_speech);
        reltv_login_bar = (RelativeLayout) findViewById(R.id.reltv_login_bar);
        btn_main_login = (Button) findViewById(R.id.btn_main_login);
        actionView = (ActionView) findViewById(R.id.action);
    }
    //viewpager初始化（设置内容）
    private void initViewPager() {
        viewPager = (ViewPager) findViewById(R.id.vpgr_main);
        pages = new ArrayList<View>();
        LayoutInflater inflater = getLayoutInflater();
        page1 = inflater.inflate(R.layout.actv_wo_de, null);
        page2 = inflater.inflate(R.layout.actv_wo_de, null);
        page3 = inflater.inflate(R.layout.actv_wo_de, null);
        pages.add(page1);
        pages.add(page2);
        pages.add(page3);
        PagerAdapter pagerAdapter = new PagerAdapter() {
            @Override
            public int getCount() {
                return pages.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object
                    object) {
                container.removeView(pages.get(position));
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(pages.get(position));
                return pages.get(position);
            }
        };
        viewPager.setAdapter(pagerAdapter);

    }
    //监听器绑定
    private void bindListeners(){
        txt_wo_de.setOnClickListener(this);
        txt_yin_yue_guan.setOnClickListener(this);
        txt_fa_xian.setOnClickListener(this);
    }
}
